#!/kunden/homepages/23/d152203319/htdocs/local/bin/python

# $Id: shoebox.cgi 11 2006-10-02 06:55:28Z razvanm $

import cgi, dumbdbm, cPickle, os, Cookie
from xml.dom.minidom import parse, parseString
from urllib import urlopen
from datetime import datetime

def format_mainpic(pic):
	img = pic["sizes"][size]
	return "<a href='%s'><img src='%s' alt='%s' title='%s' width='%s' width='%s' class='mainpic'/></a>" % \
               (pic["url"], img["url"], pic["title"], pic["title"], img["w"], img["h"])

def format_thumb(pic):
	thumb = pic["sizes"]["Thumbnail"]
	return "<a href='?id=%s'><img src='%s' alt='%s' title='%s' width='%s' width='%s' class='thumbpic'/></a>" % \
               (pic["id"], thumb["url"], pic["title"], pic["title"], thumb["w"], thumb["h"])

def format_comments(pic):
  	return "<a id='comments' href='%s'>Comments(%s)</a>" % \
               (pic["url"], pic["comments"])

def format_ts(ts):
	dt = datetime.fromtimestamp(ts)
	return dt.strftime("%d %B %Y")

form = cgi.FieldStorage()
id = form.getfirst("id", None)

print "Content-Type: text/html\n"

version = "$Date: 2006-10-02 06:55:28 +0000 (Mon, 02 Oct 2006) $"
pics = cPickle.load(file("data/shoebox.pickle"))
pics_date = {}
for pic in pics.values():
#	print pic["id"], format_ts(pic["date"])
	pics_date[int(pic["date"])] = pic
dates = pics_date.keys()
dates.sort()
dates.reverse()
#for d in dates:
#	print datetime.fromtimestamp(d).ctime(), pics_date[d]["id"]
if not id:
	id = pics_date[dates[0]]["id"]
#print "DEBUG:", pics[id]

print """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>The Shoebox</title>
  <link rel="stylesheet" type="text/css" href="css/white.css" media="screen" title="white" />
  <link rel="alternate stylesheet" type="text/css" href="css/black.css" media="screen" title="black" />
  <script src="css/styleswitcher.js" type="text/javascript"></script>
  <link rel="alternate" type="application/rss+xml" title="RSS" href="http://razvan.musaloiu.com/shoebox/rss" />
</head>
"""
if "bestonblack" in pics[id]["tags"]:
	print """<body onload="setActiveStyleSheet('black');">"""
elif "bestonwhite" in pics[id]["tags"]:
	print """<body onload="setActiveStyleSheet('white');">"""
else:
	print "<body>"

cookie = Cookie.Cookie()
size = "Medium"
if "HTTP_COOKIE" in os.environ:
	cookie.load(os.environ["HTTP_COOKIE"])
	if ("size" in cookie) and \
	   cookie["size"].value == "Large":
		if "Large" not in pics[id]["sizes"]:
			size = "Original"
		else:
			size = "Large"

print """
<div align="center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
  <td colspan="2" align="center" valign="middle">
    <div class="topnav">
      <a href="http://flickr.com/photos/razvanm/tags/shoebox/" title='""" + "%s" % len(pics) + """ pictures so far'>archives</a>&nbsp;&nbsp;
      <a href="http://www.flickr.com/photos/razvanm">flickr</a>&nbsp;&nbsp;
      <a href="http://www.flickr.com/people/razvanm/">about</a>&nbsp;&nbsp;
      <a href="http://razvan.musaloiu.com/shoebox/rss">rss</a>&nbsp;&nbsp;
      <a href="#" onclick="setActiveStyleSheet('white'); return false;" title="select white theme">white</a>/<a href="#" onclick="setActiveStyleSheet('black'); return false;" title="select black theme">black</a>&nbsp;&nbsp;
      <a href="#" onclick="setActiveSize('Small'); return false;" title="select small size">small</a>/<a href="#" onclick="setActiveSize('Large'); return false;" title="select large size">large</a>
    </div>
  </td>
</tr>
<tr>
  <td align="left" valign="bottom"></td>
  <td align="right" valign="bottom"></td>
</tr>
<tr>
  <td colspan="2" align="center">
    """ + format_mainpic(pics[id]) + """
  </td>
</tr>
<tr>
  <td align="left" valign="top"><span id="title">""" + pics[id]["title"] + """</span></td>
  <td align="right">
    <div style="color: #888888; font-style: italic;">""" + format_ts(pics[id]["date"]) + """</div>
    """ + format_comments(pics[id]) + """
  </td>
</tr>
<tr>
  <td colspan="2" style='width:""" + "%d" % (int(pics[id]["sizes"][size]["w"])-20) + """px;'>
    <div id="desc">""" + pics[id]["desc"] + """</div>
  </td>
</tr>
</table>

<div style="padding: 10px">
"""

j = dates.index(pics[id]["date"])
if j-2 >= 0:
	j -= 2
else:
	j = 0	
jmax = j + 4
if jmax >= len(dates):
	jmax = len(dates) - 1
	j = jmax - 4
for i in range(jmax, j-1, -1):
	print format_thumb(pics_date[dates[i]])

print """</div>
</div>

<div id="shoeboxlink" style="float: right;">
  <a href="http://code.google.com/p/the-shoebox/">The Shoebox</a>
</div>
<div id="photosites" style="text-align: center;">
  <a href="http://photos.vfxy.com/viewblog.php?VFXYid=862">vfxy.com</a> /
  <a href="http://www.photoblogs.org/profile/razvan.musaloiu.com/">photoblogs.org</a> /
  <a href="http://www.coolphotoblogs.com/?do=profile&id=1146">coolphotoblogs.com</a> /
  <a href="http://www.bloguri-foto.com/detalii-136,1,the-shoebox.html">bloguri-foto.com</a>
</div>

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-319751-3";
urchinTracker();
</script>

<script src="utracker.js" type="text/javascript"></script>

</body>

</html>"""
